//
//  StateTable.m
//  OperationPro
//
//  Created by yaxue wang on 16/11/4.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "StateTable.h"
#import "StateTableCell.h"
#import "StateModel.h"
@implementation StateTable
- (void)initView{
    [super initView];
    [self setShowNoDataNotice:@"暂无内容"];
    self.backgroundColor =BackColor;
    self.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.showsVerticalScrollIndicator =NO;
    [self registerCell:[StateTableCell class] withModel:[StateModel class]];
    [self setDataWithUrl:@"message_showTaskNotify" andParam:nil];
    [self setTableActionType:CTableActionType_Both];
    [self loadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     return self.mainData.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StateModel *model = [self.mainData objectAtIndex:indexPath.row];
    CGSize labelsize = CGSizeZero;
    CGSize namesize = CGSizeZero;
    NSString *name =model.S_title;
    NSString *context =model.S_brief;
    
    
    if ([context isEqualToString:@""]) {
    }else
    {
        namesize=[name sizeWithFont:F(14) constrainedToSize:CGSizeMake(WindowWidth-66, 900)];
        labelsize = [context sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-66, 900)];
    }
    return MAX(labelsize.height+namesize.height+82 , 130);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellID = [NSString stringWithFormat:@"Cell_%@", NSStringFromClass([StateModel class])];
    StateTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[StateTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    StateModel *model = [self.mainData objectAtIndex:indexPath.row];
    [cell setModel:model];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    StateModel *stateModel = [self.mainData objectAtIndex:indexPath.row];
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    if (stateModel.S_purchase_uuid==nil) {
        [dic setObject:@"" forKey:@"purchase_uuid"];

    }else
    {
        [dic setObject:stateModel.S_purchase_uuid forKey:@"purchase_uuid"];

    }
    if(stateModel.S_content==nil||[stateModel.S_content isEqualToString:@""])
    {
        
    }else
    {
        [CClass HttpRequestTask:YES withUrl:@"home_orderInfo" withData:@{@"uuid":stateModel.S_content} notifyError:YES onSuccess:^(BaseModel *model) {
            //    order_type 1 - 报修单  2 - 巡检单
            int num=[[model.B_data objectForKey:@"order_type"] integerValue];
            if (num==1) {
                //报修单
                    [dic setObject:stateModel.S_content forKey:@"warningUuid"];
                    [CClass HttpRequestTask:YES withUrl:@"warninglist_getOrderInfo" withData:@{@"warningUuid":stateModel.S_content} notifyError:YES onSuccess:^(BaseModel *model) {
                        [dic setObject:[model.B_data objectForKey:@"state"] forKey:@"state"];
                        if (self.gotoBaoxiu) {
                            self.gotoBaoxiu(dic);
                        }
            
                        
                    } onError:^(BaseModel *model) {
                        
                    }];
            }else
            {
                NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
                [dataDic setObject:stateModel.S_content forKey:@"uuid"];
                [CClass HttpRequestTask:YES withUrl:@"inspection_getInfo" withData:@{@"uuid":stateModel.S_content} notifyError:YES onSuccess:^(BaseModel *model) {
                    [dataDic setObject:[model.B_data objectForKey:@"state"] forKey:@"state"];
                    //               //巡检单
                        if (self.gotoXunjian) {
                            self.gotoXunjian(dataDic);
                        }
                    
                } onError:^(BaseModel *model) {
                    
                }];

                

            }
        } onError:^(BaseModel *model) {
            
        }];
        

    }
    
}


@end
